package com.tbmob.tb_h5;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tb.mob.TbManager;
import com.tb.mob.bean.Position;
import com.tb.mob.config.TbInteractionConfig;
import com.tb.mob.config.TbSplashConfig;
import com.tbmob.tb_h5.utils.SPUtils;

//开屏
public class SplashActivity extends AppCompatActivity {

    // 设置一个变量来控制当前开屏页面是否可以跳转，
    // 当开屏为普链类时，点击会打开一个落地页，此时开发者还不能打开自己的App主页。
    // 当从落地页返回以后，才可以跳转到开发者自己的App主页；
    // 当开屏是App类时只会下载App。
    private boolean canJump;

    private FrameLayout mSplashContainer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题栏
        //getSupportActionBar().hide();// 隐藏ActionBar
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 隐藏状态栏

        setContentView(R.layout.activity_splash);
        mSplashContainer = (FrameLayout) findViewById(R.id.splash_container);

        if (ConfigInfo.sp_switch) {
            if (ConfigInfo.is_splash) {
                loadSplash();
            } else {
                loadInteraction();
            }
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    next();
                }
            }, 3000);
        }
        //test();
    }

    private void test() {
        String h5url = SPUtils.getH5URL(this.getApplicationContext());
        if (!TextUtils.isEmpty(h5url)) {
            ConfigInfo.h5Url = h5url;
        }
        TextView a_sp_tv = findViewById(R.id.a_sp_tv);
        String s = "当前url: " + ConfigInfo.h5Url;
        a_sp_tv.setText(s);
        EditText a_sp_et = findViewById(R.id.a_sp_et);
        findViewById(R.id.a_sp_btn_sev).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ss = a_sp_et.getText().toString().trim();
                if (TextUtils.isEmpty(ss)) {
                    Toast.makeText(SplashActivity.this, "url为空，保存失败", Toast.LENGTH_SHORT).show();
                    return;
                }
                ConfigInfo.h5Url = ss;
                SPUtils.setH5URL(SplashActivity.this.getApplicationContext(), ss);
                String s = "当前url: " + ss;
                a_sp_tv.setText(s);
            }
        });
        findViewById(R.id.a_sp_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        next();
        canJump = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        canJump = false;
    }

    private void next() {
        if (canJump) {
            //跳转页面的逻辑
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            canJump = true;
        }
    }

    private void loadInteraction() {
        TbInteractionConfig config = new TbInteractionConfig.Builder()
                .codeId(ConfigInfo.interactionCodeId)
                .viewWidth(600)
                .build();
        TbManager.loadInteraction(config, this, new TbManager.InteractionLoadListener() {
            @Override
            public void onFail(String s) {
                //加载失败
                Toast.makeText(SplashActivity.this, s, Toast.LENGTH_SHORT).show();
                next();
            }

            @Override
            public void onExposure(Position position) {

            }

            @Override
            public void getSDKID(Integer integer, String s) {

            }

            @Override
            public void onClicked() {

            }

            @Override
            public void onDismiss() {
                //被关闭
                next();
            }

            @Override
            public void onVideoReady() {
                //视频准备就绪开始播放（非视频不回调）
            }

            @Override
            public void onVideoComplete() {
                //视频播放完成（非视频不回调）
            }
        });
    }

    //加载开屏
    private void loadSplash() {
        TbSplashConfig config = new TbSplashConfig.Builder()
                .codeId(ConfigInfo.splashCodeId)//平台申请的代码位id
                .container(mSplashContainer)//承载视图的容器。可⾃定义⾼度⽐例,但不能低于0.75
                .build();
        TbManager.loadSplash(config, this, new TbManager.SplashLoadListener() {
            @Override
            public void onFail(String s) {
                //加载失败
                Toast.makeText(SplashActivity.this, s, Toast.LENGTH_SHORT).show();
                next();
            }

            @Override
            public void onTick(long l) {

            }

            @Override
            public void onClicked() {

            }

            @Override
            public void onDismiss() {
                //被关闭
                next();
            }

            @Override
            public void onExposure(Position position) {

            }
        });
    }
}
